/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Observable;

/**
 *
 * @author Administrator
 */
public class EmployeeManager extends Observable {

    public boolean addEmployee(Employee employee) {
        boolean ret = employees.add(employee);
        this.setChanged();
        this.notifyObservers();
        return ret;
    }

    public boolean delEmployee(Employee employee) {
        Employee temp;
        boolean ret = false;
        for (int i = 0; i < employees.size(); i++) {
            temp = employees.get(i);
            if (temp == employee) {
                employees.remove(i);
                ret = true;
                break;
            }
        }
        this.setChanged();
        this.notifyObservers();
        return ret;
    }

    public void clearEmployees() {
        employees.clear();
        this.setChanged();
        this.notifyObservers();
    }

    public Employee getEmployeeById(String id) {
        Employee temp;

        for (int i = 0; i < employees.size(); i++) {
            temp = employees.get(i);
            if (temp.getId().equals(id)) {
                return temp;
            }
        }

        return null;
    }

    public Employee getEmployeeByName(String name) {
        Employee temp;

        for (int i = 0; i < employees.size(); i++) {
            temp = employees.get(i);
            if (temp.getName().equals(name)) {
                return temp;
            }
        }

        return null;
    }

    public boolean saveToFile(String path) {
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        boolean retCode = false;
        try {
            fos = new FileOutputStream(path);
            oos = new ObjectOutputStream(fos);
            for (int i = 0; i < employees.size(); ++i) {
                oos.writeObject(employees.get(i));
            }
            retCode = true;
        } catch (Exception e) {
            System.out.println("Exception during writing file:" + e.toString());
        }

        try {
            if (oos != null) {
                oos.close();
            }
            if (fos != null) {
                fos.close();
            }
        } catch (IOException e) {
            System.out.println("Exception during closing file:" + e.toString());
        }

        this.setChanged();
        this.notifyObservers();

        return retCode;
    }

    public boolean readFromFile(String path) {
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try {
            fis = new FileInputStream(path);
            ois = new ObjectInputStream(fis);

            //delete old ones
            employees.clear();
            while (true) {
                Employee temp = (Employee) ois.readObject();
                employees.add(temp);
            }

        } catch (EOFException e) {
            //end of file
            try {
                if (ois != null) {
                    ois.close();
                }
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException ex) {
                System.out.println("Exception during closing file:" + ex.toString());
            }
        } catch (Exception e) {
            System.out.println("Exception during serialization:" + e.toString());
            return false;
        }

        this.setChanged();
        this.notifyObservers();

        return true;
    }

    //Singleton
    public static EmployeeManager getInstance() {
        if (_instance == null) {
            _instance = new EmployeeManager();
        }
        return _instance;
    }
    private static EmployeeManager _instance = null;

    //Getter & Setter
    public ArrayList<Employee> getEmployees() {
        return employees;
    }

    public void setEmployees(ArrayList<Employee> employees) {
        this.employees = employees;
    }

    public Employee getActivatedEmployee() {
        return activatedEmployee;
    }

    public void setActivatedEmployee(Employee activatedEmployee) {
        this.activatedEmployee = activatedEmployee;
    }
    private ArrayList<Employee> employees = new ArrayList<Employee>();
    private Employee activatedEmployee;
}
